package com.xujiang.myblog.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xujiang.common.util.ToolBarHelper;
import com.xujiang.myblog.Base.CPRXFragment;
import com.xujiang.myblog.R;
import com.xujiang.myblog.adapter.FriendFragmentAdapter;
import com.xujiang.myblog.entity.FriendEntity;
import com.xujiang.myblog.util.L;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xujiang on 2017/4/12.
 */

public class FriendFragment extends CPRXFragment {

    @BindView(R.id.RCV_Friend_Fragment)
    RecyclerView RCVFriendFragment;

    private FriendFragmentAdapter friendFragmentAdapter;
    private List<FriendEntity> list;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friend, container, false);
        ButterKnife.bind(this, view);
        RCVFriendFragment.setHasFixedSize(true);
        RCVFriendFragment.setLayoutManager(new LinearLayoutManager(getContext()));
        initAdapter();
        initToolbar(view);
        L.i(TAG, "FriendFragment Blog Fragment");
        return view;
    }

    private void initToolbar(View view) {
        ToolBarHelper toolbarhelper = ToolBarHelper.with(view);
        Toolbar toolbar = toolbarhelper.getToolbar();
        try {
            setAll(toolbar, R.mipmap.ic_launcher, R.mipmap.user_portrait_hint_sm,
                    "Title test can show how long", "SubTitle test can show how long",
                    R.menu.base_toolbar_menu);
            setDefaultOnclicklistener(toolbar);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initAdapter() {

        friendFragmentAdapter = new FriendFragmentAdapter(R.layout.item_rlv_friendfragment, data());
        // 默认提供5种方法（渐显、缩放、从下到上，从左到右、从右到左）
        friendFragmentAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN); //在加载时打开动画
        friendFragmentAdapter.setNotDoAnimationCount(8); ////设置第一页加载数量

        RCVFriendFragment.setAdapter(friendFragmentAdapter);

    }

    private List<FriendEntity> data() {
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            FriendEntity friendEntity = new FriendEntity();
            friendEntity.setImageNumberUrl("http://img1.3lian.com/2015/w7/98/d/22.jpg");
            friendEntity.setPortraitUrl("http://img1.3lian.com/2015/w7/98/d/22.jpg");
            friendEntity.setMessage("Message");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间       
            String str = formatter.format(curDate);
            friendEntity.setTime(str);
            friendEntity.setTitleName("Title");
            list.add(friendEntity);
        }
        return list;

    }

    //fragment的显示监听
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Log.i("hepan", "显示封面2");
        }
    }
}
